from bottle import Bottle,run,request,template,view,hook,get,put,redirect,response,hook
import threading
from threading import Timer
import commands
import os
import datetime
import datetime as dt
import md5
import hashlib
import time
import json
import types
import numpy as np
import wtojson
import codecs
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

app = Bottle()

@app.route('/',method = "GET")
def index():
    return template('index')

@app.route('/TranToFront',method = ["POST","GET"])
def TranToFront():
    filename = "infojson.json"
    f_obj = open(filename)
    jsondata = json.load(f_obj)
    jstr = json.dumps(jsondata)
    return jstr
    
@app.route('/startpush',method = ["POST","GET"])
def startpush():
    nid = ''
    filename = 'infojson.json'
    file=open(filename,'r')
    for line in file.readlines():
        dic=json.loads(line)
        nid = len(dic)+1

    bname = request.forms.get("pushplace")
    bads = request.forms.get("pushads")
    bbit = request.forms.get("pushbit")
    bsrc = request.forms.get("srcRTMP")
    bgs = request.forms.get("pushgs")
    btl = request.forms.get("pushtl")
    obj={}
    
    tnow=time.strftime('%Y%m%d%H%M%S')
    mt = hashlib.md5()
    mt.update(tnow)
    mid=mt.hexdigest()
    obj["id"]=mid
    obj["name"]=bname
    obj["rtrate"]=bbit
    obj["ads"]=bads
    obj["src"]=bsrc
    obj["gs"]=bgs
    obj["tl"]=btl
    wtojson.write_json(obj)
    #print("OK!")
    return redirect('/')

@app.route('/delrt',method = ["POST","GET"])
def delrt():
    deldata = request.forms.get("Delinput")
    try:
        wtojson.del_json(deldata)
    finally:
        return redirect('/')


run(app,host = '192.168.1.1', port = 1080 ,debug=True)











































